<?php
echo '<pre>';
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '45,6', '789'),
    array('" aaa "', '" bbb "')
);
  $hostname  = 'localhost';
  $username = 'root';
  $password = '';
  $db = 'nb158d';
// establish connection with mysql
mysql_connect($hostname, $username, $password) or die(mysql_error());

// select database
mysql_select_db($db)or die(mysql_error());


$select = "SELECT * FROM `countries` ORDER BY id DESC ";
$result = mysql_query($select) or die(mysql_error());

$fp = fopen('file.csv', 'w');

$header = array('id','name','Image');

fputcsv($fp, $header, ',');

while($ans  = mysql_fetch_assoc($result)){
//foreach ($list as $fields) {
    fputcsv($fp, $ans, ',');

}

fclose($fp);
